#include "USBManagement.h"
#include <QRegExp>

#include <unistd.h>
#include <string.h>
#include <stdio.h>

void USBManagementThread::run(){
    while(true){
        if(runCount < 0)
            runCount = 0;
        if(runCount > 0){
            bool isChange = false;
            for(int i = 0; i < MAX_USB_COUNT; i++){
                msUSB[i].is_Change = false;
            }
            QFile f("/etc/mtab");
            if(!f.open(QIODevice::ReadOnly | QIODevice::Text))
            {
                qDebug() << "Open failed." << endl;
                return;
            }

            QTextStream txtInput(&f);
            QString allStr = txtInput.readAll();
            QStringList linStr;
            QRegExp rx("(/dev/sd\\w+) (/media/\\w+)");  // 匹配数字，可以提取1720和1080
            int pos = 0;
            while ((pos = rx.indexIn(allStr, pos)) != -1) {
                linStr = rx.capturedTexts();
                pos += rx.matchedLength();
                bool isFind = false;
                for(int i = 0; i < MAX_USB_COUNT; i++){
                    if(msUSB[i].is_Use == true && msUSB[i].name.compare(linStr[1]) == 0){

                        isFind = true;
                        if(msUSB[i].mount_name.compare(linStr[2]) == 0){
                            msUSB[i].is_Change = true;
                            break;
                        }
                        else{
                            msUSB[i].mount_name = linStr[2];
                            msUSB[i].is_Change = true;
                            isChange = true;
                            break;
                        }
                    }
                }
                if(isFind == false){
                    for(int i = 0; i < MAX_USB_COUNT; i++){
                        if(msUSB[i].is_Use == false){
                            msUSB[i].is_Change = true;
                            isChange = true;
                            msUSB[i].is_Use = true;
                            msUSB[i].name = linStr[1];
                            msUSB[i].mount_name = linStr[2];
                            break;
                        }
                    }
                }
            }

            for(int i = 0; i < MAX_USB_COUNT; i++){
                if(msUSB[i].is_Use == true && msUSB[i].is_Change == false){
                    msUSB[i].is_Use = false;
                    isChange = true;
                }
            }
            for(int i = 0; i < MAX_USB_COUNT; i++){
                if(msUSB[i].is_Use == true){
                    msUSB[i].serial = getSerial(msUSB[i].name);
                }
            }
            if(isChange){
                emit ChangeWindowSignal(msUSB);
            }
            f.close();
            runCount--;
        }
        sleep(2);
    }
}

void USBManagementThread::setRunCount(int i){
    runCount = i;
}

QString USBManagementThread::getSerial(QString name){
    ssize_t len;
    char buf[256], *p;
    char buf2[256];
    int i;
    QRegExp rx("\\d+");
    char *linkPath = name.replace(rx,"").replace("/dev/","/sys/block/").toLatin1().data();
    len = readlink(linkPath, buf, 256);
    buf[len] = 0;
    // printf("%s\n", buf);
    sprintf(buf2, "%s/%s", "/sys/block/", buf);
    for (i=0; i<6; i++) {
        p = strrchr(buf2, '/');
        *p = 0;
    }
    // printf("%s\n", buf2);
    strcat(buf2, "/serial");
    //qDebug() << buf2;
    // printf("opening %s\n", buf2);
    QFile f(buf2);
    if(!f.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        qDebug() << "Open failed." << endl;
        return "";
    }
    QTextStream txtInput(&f);
    QString allStr = txtInput.readAll();
    if(allStr.length() <= 0){
        qDebug() << "error:read()";
        return "";
    }
    allStr = allStr.replace(QRegExp("[\r\n]"), "");

    return allStr;
}
